package com.wjc.socket5webkit;

import java.net.UnknownHostException;

import com.wjc.httpserver.HttpServerService;
import com.wjc.httpsettings.HttpServerSettings;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class StartPageActivity extends Activity {

	private EditText proxyHost;
	private EditText proxyPort;
	private EditText username;
	private EditText password;
	private EditText localPort;
	private Button start;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        SharedPreferences  info = getSharedPreferences("Socket5Setting",0); 
        boolean flag=true;
        String proxy =null;
        int port=1080;
        String user=null;
        String passwd=null;
       if(flag){
         proxy = info.getString("proxyHost","112.121.189.69"); 
         port = info.getInt("proxyPort",153);
         user=info.getString("username", "qingshui");
         passwd=info.getString("password", "qingshui");}
       else{
         proxy = info.getString("proxyHost","192.168.137.1"); 
         port = info.getInt("proxyPort",1080);
         user=info.getString("username", "first");
         passwd=info.getString("password", "first");
       }
        int localport=info.getInt("localPort", 8080);
        HttpServerSettings.set(proxy, port, user, passwd, localport);
        this.proxyHost=(EditText)findViewById(R.id.proxyHost);
        this.proxyPort=(EditText)findViewById(R.id.proxyPort);
        this.username=(EditText)findViewById(R.id.username);
        this.password=(EditText)findViewById(R.id.password);
        this.localPort=(EditText)findViewById(R.id.localPort);
        /////-------------------------------
        this.proxyHost.setText(proxy);
        this.proxyPort.setText(String.valueOf(port));
        this.username.setText(user);
        this.password.setText(passwd);
        this.localPort.setText(String.valueOf(localport));
        //----------------------
        this.start=(Button)findViewById(R.id.start);
        this.start.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String proxy =proxyHost.getText().toString(); 
		        int port = Integer.valueOf(proxyPort.getText().toString());
		        String user=username.getText().toString();
		        String passwd=password.getText().toString();
		        int localport=Integer.valueOf(localPort.getText().toString());
		        
		        try {
					HttpServerService.setSocket5Proxy(proxy, port, user, passwd, getApplicationContext(),localport);
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				startService(new Intent("com.wjc.HTTP_SERVICE"));
				startActivity(new Intent(StartPageActivity.this,Socket5WebkitActivity.class));
				finish();
			}
        	
        });
        
    }
}
